package service4gateway

import (
	UserAction "MyBlogv2/Servers/UserAction/userAction"
	"MyBlogv2/blog-common/HttpCode"
	"MyBlogv2/blog-common/define"
	"MyBlogv2/blog-common/helper"
	"MyBlogv2/blog-common/models"
	"MyBlogv2/blog-common/response"
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/copier"
	"time"
)

func GetLeaveWordList(c *gin.Context) {
	idStr := c.Query("id")
	var leaveWordVoList []models.LeaveWordVo
	rsp, err := clientOfUserAction.GetLeaveWordList(c.Request.Context(), &UserAction.UserActionRequest{
		IdStr: idStr,
	})
	if err != nil {
		response.ResponseError(c, HttpCode.FAILURE)
		return
	}
	if rsp.Code != 200 {
		response.ResponseError(c, HttpCode.AppHttpCodeEnum(rsp.Code))
		return
	}
	copier.Copy(&leaveWordVoList, rsp.Data)
	for i, v := range rsp.Data {
		leaveWordVoList[i].CreateTime = helper.TimestampToMyTime(v.CreateTime)
	}
	response.ResponseOK(c, leaveWordVoList)
	return
}

func AddLeaveWord(c *gin.Context) {
	var content string
	userId, _ := c.Get("userId")
	if err := c.ShouldBind(&content); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
		return
	}
	rsp, err := clientOfUserAction.AddLeaveWord(c.Request.Context(), &UserAction.UserActionRequest{
		Content: content,
		UserId:  int64(userId.(int)),
	})
	if err != nil {
		response.ResponseError(c, HttpCode.FAILURE)
		return
	}
	if rsp.Code == 200 {
		response.ResponseOK(c, nil)
	}
	if rsp.Msg != "" {
		response.ResponseErrorWithMsg(c, HttpCode.AppHttpCodeEnum(rsp.Code), rsp.Msg)
	}
	response.ResponseError(c, HttpCode.AppHttpCodeEnum(rsp.Code))
	return
}

func GetLeaveWordList4Admin(c *gin.Context) {
	var leaveWordVoList []models.LeaveWordListVo
	models.Db.Model(new(models.LeaveWordListVo)).
		Table("t_leave_word lw").
		Select("lw.*,u.username as username").
		Joins("left join sys_user u on lw.user_id = u.id").
		Find(&leaveWordVoList)
	response.ResponseOK(c, leaveWordVoList)
}

func SearchLeaveWord(c *gin.Context) {
	var leaveWordVoList []models.LeaveWordListVo
	var searchLeaveWordDto models.SearchLeaveWordDto
	if err := c.ShouldBind(&searchLeaveWordDto); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
		return
	}
	db := models.Db.Model(new(models.LeaveWordListVo)).
		Table("t_leave_word lw").
		Select("lw.*,u.username as username").
		Joins("left join sys_user u on lw.user_id = u.id")
	if searchLeaveWordDto.Username != "" {
		db.Where("u.username like ?", "%"+searchLeaveWordDto.Username+"%")
	}
	if !time.Time(searchLeaveWordDto.StartTime).IsZero() {
		db.Where("lw.created_at >= ?", searchLeaveWordDto.StartTime)
	}
	if !time.Time(searchLeaveWordDto.EndTime).IsZero() {
		db.Where("lw.created_at <= ?", searchLeaveWordDto.EndTime)
	}
	if searchLeaveWordDto.IsCheck.Valid {
		db.Where("lw.is_check = ?", searchLeaveWordDto.IsCheck.Int64)
	}
	db.Find(&leaveWordVoList)
	response.ResponseOK(c, leaveWordVoList)
	return
}

func UpdateLeaveWordIsCheck(c *gin.Context) {
	var leaveWordIsCheckDto models.LeaveWordIsCheckDto
	if err := c.ShouldBind(&leaveWordIsCheckDto); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
		return
	}
	models.Db.Model(new(models.LeaveWord)).Where("id = ?", leaveWordIsCheckDto.Id).Update("is_check", leaveWordIsCheckDto.IsCheck)
	response.ResponseOK(c, nil)
	return
}
func DeleteLeaveWord(c *gin.Context) {
	var ids []int
	if err := c.ShouldBindJSON(&ids); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
		return
	}
	models.Db.Model(new(models.LeaveWord)).Where("id in (?)", ids).Delete(new(models.LeaveWord))
	models.Db.Model(new(models.Comment)).Where("type = ?", define.COMMENT_TYPE_LEAVE_WORD).Where("type_id in (?)", ids).Delete(new(models.Comment))
	models.Db.Model(new(models.Like)).Where("type = ?", define.LIKE_TYPE_LEAVE_WORD).Where("type_id in (?)", ids).Delete(new(models.Like))
	models.Db.Model(new(models.Favorite)).Where("type = ?", define.FAVORITE_TYPE_LEAVE_WORD).Where("type_id in (?)", ids).Delete(new(models.Favorite))
	response.ResponseOK(c, nil)
}
